<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库编码" prop="cwhcode">
        <el-input
          v-model="queryParams.cwhcode"
          placeholder="请输入仓库编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="cwhname">
        <el-input
          v-model="queryParams.cwhname"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门编码" prop="cdepcode">
        <el-input
          v-model="queryParams.cdepcode"
          placeholder="请输入所属部门编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic/warehouse:warehouse:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic/warehouse:warehouse:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic/warehouse:warehouse:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic/warehouse:warehouse:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仓库编码" align="center" prop="cwhcode" />
      <el-table-column label="仓库名称" align="center" prop="cwhname" />
      <el-table-column label="所属部门编码" align="center" prop="cdepcode" />
      <el-table-column label="地址" align="center" prop="cwhaddress" />
      <el-table-column label="电话" align="center" prop="cwhphone" />
      <el-table-column label="cWhValueStyle" align="center" prop="cwhvaluestyle" />
      <el-table-column label="bWhPos" align="center" prop="bwhpos" />
      <el-table-column label="最低存量" align="center" prop="iwhfundquota" />
      <el-table-column label="cMonth" align="center" prop="cmonth" />
      <el-table-column label="备注" align="center" prop="cwhmemo" />
      <el-table-column label="bFreeze" align="center" prop="bfreeze" />
      <el-table-column label="条码" align="center" prop="cbarcode" />
      <el-table-column label="bMRP" align="center" prop="bmrp" />
      <el-table-column label="bROP" align="center" prop="brop" />
      <el-table-column label="负责人" align="center" prop="cwhperson" />
      <el-table-column label="库容量" align="center" prop="krl" />
      <el-table-column label="单位编码" align="center" prop="dwbm" />
      <el-table-column label="层级" align="center" prop="cj" />
      <el-table-column label="所属单位OID" align="center" prop="ssdwoid" />
      <el-table-column label="存货档案OID" align="center" prop="jcInventoryoid" />
      <el-table-column label="上级仓库OID" align="center" prop="parentoid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic/warehouse:warehouse:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic/warehouse:warehouse:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库编码" prop="cwhcode">
          <el-input v-model="form.cwhcode" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="cwhname">
          <el-input v-model="form.cwhname" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="所属部门编码" prop="cdepcode">
          <el-input v-model="form.cdepcode" placeholder="请输入所属部门编码" />
        </el-form-item>
        <el-form-item label="地址" prop="cwhaddress">
          <el-input v-model="form.cwhaddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="电话" prop="cwhphone">
          <el-input v-model="form.cwhphone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="cWhValueStyle" prop="cwhvaluestyle">
          <el-input v-model="form.cwhvaluestyle" placeholder="请输入cWhValueStyle" />
        </el-form-item>
        <el-form-item label="bWhPos" prop="bwhpos">
          <el-input v-model="form.bwhpos" placeholder="请输入bWhPos" />
        </el-form-item>
        <el-form-item label="最低存量" prop="iwhfundquota">
          <el-input v-model="form.iwhfundquota" placeholder="请输入最低存量" />
        </el-form-item>
        <el-form-item label="cMonth" prop="cmonth">
          <el-input v-model="form.cmonth" placeholder="请输入cMonth" />
        </el-form-item>
        <el-form-item label="备注" prop="cwhmemo">
          <el-input v-model="form.cwhmemo" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="bFreeze" prop="bfreeze">
          <el-input v-model="form.bfreeze" placeholder="请输入bFreeze" />
        </el-form-item>
        <el-form-item label="条码" prop="cbarcode">
          <el-input v-model="form.cbarcode" placeholder="请输入条码" />
        </el-form-item>
        <el-form-item label="bMRP" prop="bmrp">
          <el-input v-model="form.bmrp" placeholder="请输入bMRP" />
        </el-form-item>
        <el-form-item label="bROP" prop="brop">
          <el-input v-model="form.brop" placeholder="请输入bROP" />
        </el-form-item>
        <el-form-item label="负责人" prop="cwhperson">
          <el-input v-model="form.cwhperson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="库容量" prop="krl">
          <el-input v-model="form.krl" placeholder="请输入库容量" />
        </el-form-item>
        <el-form-item label="单位编码" prop="dwbm">
          <el-input v-model="form.dwbm" placeholder="请输入单位编码" />
        </el-form-item>
        <el-form-item label="层级" prop="cj">
          <el-input v-model="form.cj" placeholder="请输入层级" />
        </el-form-item>
        <el-form-item label="所属单位OID" prop="ssdwoid">
          <el-input v-model="form.ssdwoid" placeholder="请输入所属单位OID" />
        </el-form-item>
        <el-form-item label="存货档案OID" prop="jcInventoryoid">
          <el-input v-model="form.jcInventoryoid" placeholder="请输入存货档案OID" />
        </el-form-item>
        <el-form-item label="上级仓库OID" prop="parentoid">
          <el-input v-model="form.parentoid" placeholder="请输入上级仓库OID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse } from "@/api/basic/warehouse/warehouse";

export default {
  name: "Warehouse",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库档案表格数据
      warehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cwhcode: null,
        cwhname: null,
        cdepcode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cwhcode: [
          { required: true, message: "仓库编码不能为空", trigger: "blur" }
        ],
        cwhname: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        cdepcode: [
          { required: true, message: "所属部门编码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询仓库档案列表 */
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jcWarehouseoid: null,
        cwhcode: null,
        cwhname: null,
        cdepcode: null,
        cwhaddress: null,
        cwhphone: null,
        cwhvaluestyle: null,
        bwhpos: null,
        iwhfundquota: null,
        cmonth: null,
        cwhmemo: null,
        bfreeze: null,
        cbarcode: null,
        bmrp: null,
        brop: null,
        cwhperson: null,
        krl: null,
        dwbm: null,
        cj: null,
        ssdwoid: null,
        jcInventoryoid: null,
        parentoid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jcWarehouseoid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const jcWarehouseoid = row.jcWarehouseoid || this.ids
      getWarehouse(jcWarehouseoid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jcWarehouseoid != null) {
            updateWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jcWarehouseoids = row.jcWarehouseoid || this.ids;
      this.$modal.confirm('是否确认删除仓库档案编号为"' + jcWarehouseoids + '"的数据项？').then(function() {
        return delWarehouse(jcWarehouseoids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/warehouse/warehouse/export', {
        ...this.queryParams
      }, `warehouse_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
